﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;

public partial class EditTour : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    protected void LoadData()
    {
        int tourid = int.Parse(Request["tourid"]);

        // ckfinder
        CKEditor_Description.Language = "vi";
        CKFinder.FileBrowser
        _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = "/ckfinder/";
        _FileBrowser.SetupCKEditor(CKEditor_Description);

        //fin
        CKEditor_DescriptionVN.Language = "vi";
        CKFinder.FileBrowser
        _FileBrowser1 = new CKFinder.FileBrowser();
        _FileBrowser1.BasePath = "/ckfinder/";
        _FileBrowser1.SetupCKEditor(CKEditor_DescriptionVN);
        var tour = db.Tours.Single(c => c.TourId == tourid);
        txt_Name.Text = tour.Name;
        txt_NameVN.Text = tour.NameVN;
        txt_UnitPrice.Text = tour.UnitPrice.ToString();
        txt_PriceChidren.Text = tour.PriceChidren.ToString();
        txt_PriceBaby.Text = tour.PriceBaby.ToString();
        txt_SoNgay.Text = tour.TourDate;
        txt_SoNgayVN.Text = tour.TourDateVN;
        var travel = db.Travels.Single(c => c.TourId == tourid);
        txt_DatStart.Text = travel.DateStart;
        txt_DatStartVN.Text = travel.DateStartVN;
        txt_Image_Edit.Text = tour.Image;
        var sur = db.Surcharges.Single(c => c.TourId == int.Parse(Request["tourid"]));
        txt_Singleroom.Text = sur.Singleroom.ToString();
        txt_Foreigner.Text = sur.Foreigner.ToString();
        if (tour.Activated == true)
        {
            rad_Available.SelectedIndex = 0;
        }
        else
        {
            rad_Available.SelectedIndex = 1;
        }
        if (tour.Promotion == true)
        {
            rad_Proto.SelectedIndex = 0;
        }
        else
        {
            rad_Proto.SelectedIndex = 1;
        }
        CKEditor_Description.Text = tour.Description;
        CKEditor_DescriptionVN.Text = tour.DescriptionVN;
        CKEditor_Price.Text = tour.DescriptionPrice;
        CKEditor_PriceVN.Text = tour.DescriptionPriceVN;

        ddl_Category.DataSource = db.Categories.Where(c => c.Activated == true);
        ddl_Category.DataValueField = "CategoryId";
        ddl_Category.DataTextField = "NameVN";
        ddl_Category.DataBind();
        ddl_Category.SelectedValue = tour.CategoryId.ToString();
        ddl_Category.Enabled = false;

        ck_Location.DataSource = db.Locations.Where(p => p.CategoryId == int.Parse(ddl_Category.SelectedValue.ToString()) && p.Activated == true);
        ck_Location.DataValueField = "LocationId";
        ck_Location.DataTextField = "NameVN";
        ck_Location.DataBind();

        ddl_Hotel.DataSource = db.Hotels;
        ddl_Hotel.DataValueField = "HotelId";
        ddl_Hotel.DataTextField = "Name";
        ddl_Hotel.DataBind();
        ddl_Hotel.SelectedValue = tour.HotelId.ToString();

        ddl_Transport.DataSource = db.Transports;
        ddl_Transport.DataValueField = "TransportId";
        ddl_Transport.DataTextField = "NameVN";
        ddl_Transport.DataBind();
        ddl_Transport.SelectedValue = tour.TransportId.ToString();
        // lay bang content co tourid=touid
        var content = db.LocationTours.Where(con => con.TourId == tourid);
        foreach (var ct in content)
        {
            for (int i = 0; i < ck_Location.Items.Count; i++)
            {
                if (int.Parse(ck_Location.Items[i].Value.ToString()) == ct.LocationId)
                {
                    ck_Location.Items[i].Selected = true;
                    break;
                }
            }
        }


    }

    protected void btnSaveEdit_Click(object sender, EventArgs e)
    {
            int ktrack = 0;
            for (int i = 0; i < ck_Location.Items.Count; i++)
            {
                if (ck_Location.Items[i].Selected == true)
                {
                    ktrack = 1;
                    break;
                }
            }
            if (ktrack == 1)
            {
                // tim 

                var tou = db.Tours.Single(c => c.TourId == int.Parse(Request["tourid"]));
                tou.Name = txt_Name.Text;
                tou.NameVN = txt_NameVN.Text;
                tou.UnitPrice = double.Parse(txt_UnitPrice.Text);
                tou.PriceBaby = double.Parse(txt_PriceBaby.Text);
                tou.PriceChidren = double.Parse(txt_PriceChidren.Text);
                tou.TourDate = txt_SoNgay.Text;
                tou.TourDateVN = txt_SoNgayVN.Text;
                tou.Promotion = bool.Parse(rad_Proto.SelectedValue);
                tou.Activated = bool.Parse(rad_Available.SelectedValue);
                tou.Image = txt_Image_Edit.Text;
                tou.DescriptionPrice = CKEditor_Price.Text;
                tou.DescriptionPriceVN = CKEditor_PriceVN.Text;

                tou.CategoryId = int.Parse(ddl_Category.SelectedValue);
                tou.HotelId = int.Parse(ddl_Hotel.SelectedValue);
                tou.TransportId = int.Parse(ddl_Transport.SelectedValue);
                tou.Description = CKEditor_Description.Text;
                tou.DescriptionVN = CKEditor_DescriptionVN.Text;

               
                //3 Luu
                db.SubmitChanges();
                var travel = db.Travels.Single(c => c.TourId == tou.TourId);
                travel.DateStart = txt_DatStart.Text;
                travel.DateStartVN = txt_DatStartVN.Text;
                // xoa loc bang locationtour
                var content1 = db.LocationTours.Where(con => con.TourId == int.Parse(Request["tourid"]));
                int ktraCon = 0;
                foreach (var ct in content1)
                {
                    for (int i = 0; i < ck_Location.Items.Count; i++)
                    {
                        if (ck_Location.Items[i].Selected == true && ct.LocationId == int.Parse(ck_Location.Items[i].Value.ToString()))
                        {

                            ktraCon = 1;
                            break;
                        }
                    }
                    if (ktraCon == 0)
                    {
                        var conDel = db.LocationTours.Single(c => c.LocationId == ct.LocationId && c.TourId == ct.TourId);
                        db.LocationTours.DeleteOnSubmit(conDel);
                        db.SubmitChanges();
                    }
                    else
                    {
                        ktraCon = 0;
                    }
                }
                //luu vao ban conten
                var content2 = db.LocationTours.Where(con => con.TourId == int.Parse(Request["tourid"]));

                for (int i = 0; i < ck_Location.Items.Count; i++)
                {
                    if (ck_Location.Items[i].Selected == true)
                    {
                        foreach (var ct in content2)
                        {
                            if (ct.LocationId == int.Parse(ck_Location.Items[i].Value.ToString()))
                            {
                                ktraCon = 1;
                                break;
                            }
                        }
                        if (ktraCon == 0)
                        {
                            var content = new LocationTour
                            {
                                TourId = int.Parse(Request["tourid"]),
                                LocationId = int.Parse(ck_Location.Items[i].Value)
                            };
                            db.LocationTours.InsertOnSubmit(content);
                            db.SubmitChanges();
                        }
                        else
                        {
                            ktraCon = 0;
                        }
                    }
                }
                // luu bảng phụ thu
                var sur = db.Surcharges.Single(c => c.TourId == int.Parse(Request["tourid"]));
                sur.Foreigner=int.Parse(txt_Foreigner.Text);
                sur.Singleroom=int.Parse(txt_Singleroom.Text);
                db.SubmitChanges();
                Response.Redirect("Tour.aspx");
            }
            else
            {
                Response.Write("<script> alert('Hãy chọn địa danh  du lịch trước khi lưu!');</script>");
            }
    }


    #region - Create Thumb Image -

    public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
    {
        System.Drawing.Bitmap bmpOut = null;
        try
        {
            Bitmap loBMP = new Bitmap(lcFilename);
            ImageFormat loFormat = loBMP.RawFormat;

            decimal lnRatio;
            int lnNewWidth = 0;
            int lnNewHeight = 0;

            //*** If the image is smaller than a thumbnail just return it
            if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                return loBMP;

            if (loBMP.Width > loBMP.Height)
            {
                lnRatio = (decimal)lnWidth / loBMP.Width;
                lnNewWidth = lnWidth;
                decimal lnTemp = loBMP.Height * lnRatio;
                lnNewHeight = (int)lnTemp;
            }
            else
            {
                lnRatio = (decimal)lnHeight / loBMP.Height;
                lnNewHeight = lnHeight;
                decimal lnTemp = loBMP.Width * lnRatio;
                lnNewWidth = (int)lnTemp;
            }
            bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
            Graphics g = Graphics.FromImage(bmpOut);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
            g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

            loBMP.Dispose();
        }
        catch
        {
            return null;
        }

        return bmpOut;
    }

    #endregion
    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Tour.aspx");
    }
}